import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

//import com.smartxls.ChartShape;
//import com.smartxls.WorkBook;

public class Test {

	public static void main(String args[]) {

		String saveFileName = "test";

		// Values of test parameters.
		int GENERATIONS = 500;
		int NAGENTS = 10000;
		double DISTRIBUTION = 0.1;
		double MORTALITY = 10.0;
		int COST = 5;
		int BENEFIT = 10;
		int SHOTCOUNT = 1;

		try {
			// CSV-file data collection.

			FileWriter writer = new FileWriter("./Data/" +saveFileName +".csv");
			
			for (int j = 0; j < GENERATIONS; j++) {
				writer.append(j+"");
				writer.append(',');
			}
			writer.append('\n');
			
			// *** Test with minimum distribution.
			Behavior test = new Behavior(NAGENTS, 0.01, COST, BENEFIT,
					MORTALITY, SHOTCOUNT);
			
			// Initial data.
			writer.append(String.valueOf(test.getCoopRatio()));
			writer.append(',');
			
			runOnce(test, GENERATIONS, writer);
		
			// *** Tests a number of different distributions.
			for( int i = 0; i < 9; i++) {
				
				// Construction.
				Behavior test1 = new Behavior(NAGENTS, (DISTRIBUTION + (double)((double)i/10)), COST, BENEFIT,
						MORTALITY, SHOTCOUNT);
				
				// Initial data.
				writer.append(String.valueOf(test1.getCoopRatio()));
				writer.append(',');
				
				runOnce(test1, GENERATIONS, writer);
				}
			
			// *** Test with maximum distribution.
			Behavior test2 = new Behavior(NAGENTS, 0.99, COST, BENEFIT,
					MORTALITY, SHOTCOUNT);
			
			// Initial data.
			writer.append(String.valueOf(test2.getCoopRatio()));
			writer.append(',');
			
			runOnce(test2, GENERATIONS, writer);
			
			writer.flush();
			writer.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void runOnce(Behavior test, int generations, FileWriter writer) {

		// Executing test.
		for (int i = 1; i <= generations; i++) {

			// Printing the results.
			System.out.println("*** Round " + i + " ***");

			// All prisoners interact once on average.
			test.interactSnowDrift(3, 1);

			System.out.println("         Coop ratio: " + test.getCoopRatio());

			// Agents are killed according to the above percentage.
			test.mortality();

			// The same amount of killed agents are reproduced.
			test.reproduction();

			
			try {
				// Test data.
				writer.append(String.valueOf(test.getCoopRatio()));
				writer.append(',');
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		try {
			writer.append('\n');
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
